home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / mg2a_src.zip / SYS / OSK / TTYIO.C < prev    next >
C/C++ Source or Header  |  1988-08-23  |  3KB  |  143 lines

  1. /*
  2.  *    sys/osk/ttyio.c by Robert A. Larson
  3.  *
  4.  * The functions in this file
  5.  * negotiate with the operating system for
  6.  * keyboard characters, and write characters to
  7.  * the display in a barely buffered fashion.
  8.  */
  9. #include    "def.h"
  10.  
  11. #include    <sgstat.h>
  12. #ifndef NO_DPROMPT
  13. #  include    <varargs.h>
  14. #  define S_RDY 2437            /* arbitrary user signal */
  15. #endif
  16.  
  17. #define NOBUF    512            /* Output buffer size.        */
  18.  
  19. char    obuf[NOBUF];            /* Output buffer.        */
  20. int    nobuf;
  21. struct    sgbuf    oldtty, newtty;
  22. int    nrow;                /* Terminal size, rows.        */
  23. int    ncol;                /* Terminal size, columns.    */
  24. short    ospeed;                /* Terminal speed, for termlib.l */
  25.  
  26. #ifndef NO_DPROMPT
  27. wakeup(signum)
  28. int signum;
  29. {
  30.     /* ignore the signal */
  31. }
  32. #endif
  33.  
  34. /*
  35.  * This function gets called once, to set up
  36.  * the terminal channel.
  37.  */
  38.  
  39. ttopen()
  40. {
  41.     if(_gs_opt(0, &oldtty) == -1) panic("can't get options");
  42.     ospeed = oldtty.sg_baud;
  43.     _strass(&newtty, &oldtty, sizeof(newtty));    /* newtty=oldtty; */
  44.     if(oldtty.sg_class == 0) {            /* scf */
  45.         newtty.sg_backsp=
  46.         newtty.sg_delete=
  47.         newtty.sg_echo    =
  48.         newtty.sg_alf    =
  49.         newtty.sg_pause =
  50.         newtty.sg_bspch =
  51.         newtty.sg_dlnch =
  52.         newtty.sg_eorch =
  53.         newtty.sg_eofch =
  54.         newtty.sg_rlnch =
  55.         newtty.sg_dulnch=
  56.         newtty.sg_psch    =
  57.         newtty.sg_kbich =
  58.         newtty.sg_kbach = 0;
  59. #ifndef xon_xoff
  60.         newtty.sg_xon    =
  61.         newtty.sg_xoff    = 0;
  62. #endif
  63.         if(_ss_opt(0, &newtty) == -1) panic("can't set options");
  64.         nrow = oldtty.sg_page == 0 ? NROW : oldtty.sg_page;
  65.     } else {                /* not scf, fake it */
  66.         nrow = NROW;
  67.     }
  68.     ncol = NCOL;
  69. #ifndef NO_DPROMPT
  70.     intercept(wakeup);        /* ignore signals */
  71. #endif
  72. }
  73.  
  74. /*
  75.  * This function gets called just
  76.  * before we go back home to the shell. Put all of
  77.  * the terminal parameters back.
  78.  */
  79. ttclose()
  80. {
  81.     ttflush();
  82.     if(_ss_opt(0, &oldtty) == -1) panic("can't reset options");
  83. }
  84.  
  85. /*
  86.  * Write character to the display.
  87.  * Characters are buffered up, to make things
  88.  * a little bit more efficient.
  89.  */
  90. ttputc(c)
  91. {
  92.     if (nobuf >= NOBUF)
  93.         ttflush();
  94.     obuf[nobuf++] = c;
  95. }
  96.  
  97. /*
  98.  * Flush output.
  99.  */
  100. ttflush()
  101. {
  102.     if (nobuf != 0) {
  103.         write(1, obuf, nobuf);
  104.         nobuf = 0;
  105.     }
  106. }
  107.  
  108. /*
  109.  * Read character from terminal.
  110.  * All 8 bits are returned, so that you can use
  111.  * a multi-national terminal.
  112.  */
  113. ttgetc()
  114. {
  115.     char    buf[1];
  116.  
  117.     while (read(0, &buf[0], 1) != 1)
  118.         ;
  119.     return (buf[0] & 0xFF);
  120. }
  121.  
  122. int typeahead()
  123. {
  124.   return _gs_rdy(0) > 0;
  125. }
  126.  
  127. panic(s) char *s; {
  128.   _ss_opt(0, &oldtty);            /*  ignore errors */
  129.   fputs("Panic: ", stdout);        /* avoid printf, don't load all that */
  130.   puts(s);
  131.   exit(1);
  132. }
  133.  
  134. #ifndef NO_DPROMPT
  135. ttwait() {
  136.   if(_gs_rdy(0) > 0) return FALSE;    /* already something waiting */
  137.   _ss_ssig(0, S_RDY);            /* wake me when you have something */
  138.   if(sleep(2)!=0) return FALSE;        /* sleep interupted */
  139.   _ss_rel(0);
  140.   return TRUE;
  141. }
  142. #endif
  143.